package com.tongan.generate.onetomany;

import com.tongan.generate.BaseCodeFactory;
import com.tongan.generate.ICallBack;
import com.tongan.util.CodeResourceUtil;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class CgformCodeFactoryOneToMany extends BaseCodeFactory {
   private ICallBack callBack;
   private String projectPath;

   public void generateFile(String templateFileName, String type, Map data) throws TemplateException, IOException {
      try {
         String entityPackage = data.get("entityPackage").toString();
         String entityName = data.get("entityName").toString();
         String fileNamePath = this.getCodePath(type, entityPackage, entityName);
         String fileDir = StringUtils.substringBeforeLast(fileNamePath, "/");
         Template template = this.getConfiguration().getTemplate(templateFileName);
         FileUtils.forceMkdir(new File(fileDir + "/"));
         Writer out = new OutputStreamWriter(new FileOutputStream(fileNamePath), CodeResourceUtil.SYSTEM_ENCODING);
         template.process(data, out);
         out.close();
      } catch (TemplateException var10) {
         var10.printStackTrace();
         throw var10;
      } catch (IOException var11) {
         var11.printStackTrace();
         throw var11;
      }
   }

   public void generateFileUserDefined(String templateFileName, String type, Map data) throws TemplateException, IOException {
      try {
         String entityPackage = data.get("entityPackage").toString();
         String entityName = data.get("entityName").toString();
         String fileNamePath = this.getCodePath(type, entityPackage, entityName);
         String fileDir = StringUtils.substringBeforeLast(fileNamePath, "/");
         Template template = this.getConfigurationUserDefined().getTemplate(templateFileName);
         FileUtils.forceMkdir(new File(fileDir + "/"));
         Writer out = new OutputStreamWriter(new FileOutputStream(fileNamePath), CodeResourceUtil.SYSTEM_ENCODING);
         template.process(data, out);
         out.close();
      } catch (TemplateException var10) {
         var10.printStackTrace();
         throw var10;
      } catch (IOException var11) {
         var11.printStackTrace();
         throw var11;
      }
   }

   public String getProjectPath() {
      return this.projectPath;
   }

   public String getClassPath() {
      String path = this.getClass().getResource("/").getPath();
      return path;
   }

   public static void main(String[] args) {
      System.out.println(Thread.currentThread().getContextClassLoader().getResource("./").getPath());
   }

   public String getTemplatePath() {
      String path = this.getClassPath() + CodeResourceUtil.TEMPLATEPATH;
      return path;
   }

   public String getCodePath(String type, String entityPackage, String entityName) {
      String path = this.getProjectPath();
      String codePath = "";
      if (this.packageStyle != null && CodeResourceUtil.PACKAGE_SERVICE_STYLE.equals(this.packageStyle)) {
         codePath = this.getCodePathServiceStyle(path, type, entityPackage, entityName);
      } else {
         codePath = this.getCodePathProjectStyle(path, type, entityPackage, entityName);
      }

      return codePath;
   }

   public void invoke(String templateFileName, String type) throws TemplateException, IOException {
      new HashMap();
      Map data = this.callBack.execute();
      this.generateFile(templateFileName, type, data);
   }

   public void invokeUserDefined(String templateFileName, String type) throws TemplateException, IOException {
      new HashMap();
      Map data = this.callBack.execute();
      this.generateFileUserDefined(templateFileName, type, data);
   }

   public ICallBack getCallBack() {
      return this.callBack;
   }

   public void setCallBack(ICallBack callBack) {
      this.callBack = callBack;
   }

   public void setProjectPath(String projectPath) {
      this.projectPath = projectPath;
   }
}
